using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
namespace MicroMsg.Scene.Image
{
	public class NetSceneDownloadImg : NetSceneBaseEx<GetMsgImgRequest, GetMsgImgResponse, GetMsgImgRequest.Builder>
	{
		private const string TAG = "NetSceneDownloadImage";
		public const int DOSCENE_SUCCESS = 1;
		public const int DOSCENE_SYS_ERR = 0;
		public const int DOSCENE_LOADING_ERR = -1;
		private string mUserName = "";
		private int ONE_PACK_DOWNLOADSIZE = 8192;
		public DownloadImgContext mDownImgContext;
		public int mCompressType;
		public event onSceneDownFinishedDelegate mOnDownSceneFinished;
		public int doScene(int imgMsgSvrId, string toTalker, int compressType = 0)
		{
			return new NetSceneDownloadImg().doSceneEx(imgMsgSvrId, toTalker, compressType);
		}
		private int doSceneEx(int imgMsgSvrId, string toTalker, int compressType = 0)
		{
			if (imgMsgSvrId <= 0)
			{
				Log.smethod_1("NetSceneDownloadImage", "DownLoad image fail because of imgMsgSvrId <= 0");
				return 0;
			}
			this.mCompressType = compressType;
			return DownloadImgService.startDoSence(imgMsgSvrId, this, toTalker, compressType);
		}
		public void doScene(DownloadImgContext downImgContext)
		{
			if (downImgContext == null || downImgContext.fileStream == null)
			{
				return;
			}
			this.mDownImgContext = downImgContext;
			Account curAccount = AccountMgr.getCurAccount();
			this.mUserName = curAccount.strUsrName;
			this.mDownImgContext.mImgInfo.strToUserName = this.mUserName;
			this.mDownImgContext.mStatus = 1;
			if (this.mDownImgContext.mImgInfo.strFromUserName != null && this.mUserName != null)
			{
				int nTransDataLen = this.mDownImgContext.mImgInfo.nTransDataLen;
				string progressString = this.getProgressString(nTransDataLen);
				ChatMsg bySvrID = StorageMgr.chatMsg.getBySvrID(this.mDownImgContext.mImgInfo.strFromUserName, this.mDownImgContext.mImgInfo.nMsgSvrID);
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOADIMG_PROGRESS, new ParamImgDownProgress(progressString, bySvrID.nMsgLocalID, this.mDownImgContext.mImgInfo.strFromUserName), null);
				this.doScene();
				return;
			}
			Log.smethod_1("NetSceneDownloadImage", "mDownImgContext.mImgInfo.strFromUserName is null");
		}
		private void doScene()
		{
			if (!string.IsNullOrEmpty(this.mDownImgContext.mImgInfo.strFromUserName) && !string.IsNullOrEmpty(this.mUserName))
			{
				base.beginBuilder();
				this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(10);
				this.mBuilder.FromUserName = Util.toSKString(this.mDownImgContext.mImgInfo.strFromUserName);
				this.mBuilder.ToUserName = Util.toSKString(this.mUserName);
				this.mBuilder.TotalLen = (uint)this.mDownImgContext.mImgInfo.nTotalDataLen;
				this.mBuilder.StartPos = (uint)this.mDownImgContext.mImgInfo.nTransDataLen;
				this.mBuilder.DataLen = (uint)this.ONE_PACK_DOWNLOADSIZE;
				this.mBuilder.MsgId = (uint)this.mDownImgContext.mImgInfo.nMsgSvrID;
				this.mBuilder.CompressType = (uint)this.mCompressType;
				this.mSessionPack.mCmdID = 10;
				this.mSessionPack.mConnectMode = 2;
				this.mSessionPack.mHttpRecvProgress += new OnHttpReceivedProgressDelegate(this.OnHttpReceivedProgress);
				base.endBuilder();
				this.mDownImgContext.mStatus = 2;
				return;
			}
			Log.smethod_1("NetSceneDownloadImage", "DownLoadImg doScene failed because of Param error");
			this.setDownloadError();
		}
		private void OnHttpReceivedProgress(int length)
		{
			int nTotalDataLen = this.mDownImgContext.mImgInfo.nTotalDataLen;
			if (nTotalDataLen <= 0)
			{
			}
			int byteLen = this.mDownImgContext.mImgInfo.nTransDataLen + length;
			string progressString = this.getProgressString(byteLen);
			ChatMsg bySvrID = StorageMgr.chatMsg.getBySvrID(this.mDownImgContext.mImgInfo.strFromUserName, this.mDownImgContext.mImgInfo.nMsgSvrID);
			EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOADIMG_PROGRESS, new ParamImgDownProgress(progressString, bySvrID.nMsgLocalID, this.mDownImgContext.mImgInfo.strFromUserName), null);
		}
		protected override void onFailed(GetMsgImgRequest request, GetMsgImgResponse response)
		{
			Log.smethod_1("NetSceneDownloadImage", "system error DownLoad image failed !");
			this.setDownloadError();
		}
		protected override void onSuccess(GetMsgImgRequest request, GetMsgImgResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneDownloadImage", "Download image failed because of net error");
				this.setDownloadError();
				return;
			}
			if (response.DataLen <= 0u)
			{
				Log.smethod_1("NetSceneDownloadImage", "Download image failed because of response.DataLen <= 0");
				this.mDownImgContext.mStatus = 5;
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOADIMG_COMPLETE, new ParamReturn(100, this.mDownImgContext.mImgInfo.nMsgLocalID, this.mDownImgContext.mImgInfo.strFromUserName, this.mDownImgContext.mImgInfo.strImagePath), null);
				this.doSceneFinished();
				return;
			}
			if (response.Data != null)
			{
				if (response.DataLen == response.Data.ILen)
				{
					uint arg_BC_0 = response.StartPos;
					if (response.StartPos + response.DataLen > response.TotalLen)
					{
						Log.smethod_1("NetSceneDownloadImage", "Download image failed because of server image length error!");
						this.setDownloadError();
						return;
					}
					if ((ulong)response.StartPos != (ulong)((long)this.mDownImgContext.mImgInfo.nTransDataLen))
					{
						Log.smethod_1("NetSceneDownloadImage", "Download image failed because of position error!");
						this.setDownloadError();
						return;
					}
					if (response.TotalLen <= 0u)
					{
						Log.smethod_1("NetSceneDownloadImage", "Download image failed because of TotalLen error!");
						this.setDownloadError();
						return;
					}
					this.mDownImgContext.mImgInfo.nTotalDataLen = (int)response.TotalLen;
					this.writeBufIntoStream(response.Data.Buffer.ToByteArray(), response.StartPos, response.DataLen);
					this.mDownImgContext.mImgInfo.nTransDataLen = (int)(response.StartPos + response.DataLen);
					this.mDownImgContext.updateDownloadImgContext();
					if (!this.isDownloadCompleted())
					{
						this.doScene();
						return;
					}
					Log.smethod_4("NetSceneDownloadImage", "Download image Complete!");
					this.mDownImgContext.mStatus = 5;
					ChatMsg bySvrID = StorageMgr.chatMsg.getBySvrID(this.mDownImgContext.mImgInfo.strFromUserName, this.mDownImgContext.mImgInfo.nMsgSvrID);
					if (bySvrID == null)
					{
						Log.smethod_1("NetSceneDownloadImage", "Down load image msg == null");
						return;
					}
					if (request.CompressType == 1u)
					{
						bySvrID.strContent = ChatMsgMgr.rePackImageLenXml((int)response.TotalLen);
						StorageMgr.chatMsg.modifyMsg(bySvrID);
					}
					EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOADIMG_COMPLETE, new ParamReturn(100, bySvrID.nMsgLocalID, this.mDownImgContext.mImgInfo.strFromUserName, this.mDownImgContext.mImgInfo.strImagePath), null);
					this.doSceneFinished();
					return;
				}
			}
			Log.smethod_1("NetSceneDownloadImage", "Download image failed because of server param error!");
			this.setDownloadError();
		}
		public string getProgressString(int byteLen)
		{
			string text = ((double)byteLen * 1.0 / 1024.0).ToString();
			int num = text.IndexOf(".", 0);
			if (num == -1)
			{
				return text;
			}
			return text.Substring(0, num + 2);
		}
		private bool isDownloadCompleted()
		{
			return this.mDownImgContext.mImgInfo.nTransDataLen == this.mDownImgContext.mImgInfo.nTotalDataLen && this.mDownImgContext.mImgInfo.nTotalDataLen != 0;
		}
		private void writeBufIntoStream(byte[] byte_0, uint startPos, uint ndataLen)
		{
			if (byte_0 == null)
			{
				return;
			}
			try
			{
				this.mDownImgContext.fileStream.Seek((long)((ulong)startPos), 0);
				this.mDownImgContext.fileStream.Write(byte_0, 0, (int)ndataLen);
			}
			catch (Exception ex)
			{
				Log.smethod_1("NetSceneDownloadImage", ex.get_Message());
			}
		}
		public void setDownloadError()
		{
			this.mDownImgContext.mStatus = 4;
			EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOADIMG_FAIL, new ParamReturn(-1, this.mDownImgContext.mImgInfo.nMsgLocalID, this.mDownImgContext.mImgInfo.strFromUserName), null);
			this.doSceneFinished();
		}
		public void doSceneFinished()
		{
			Log.smethod_4("NetSceneDownloadImage", "netscene do finished. ");
			if (this.mOnDownSceneFinished != null)
			{
				this.mOnDownSceneFinished(this.mDownImgContext);
			}
			this.mDownImgContext.updateDownloadImgContext();
			this.mDownImgContext.fileStream.Close();
		}
	}
}
